package com.star.bookstore.member.controller;

import com.star.bookstore.common.utils.PageUtils;
import com.star.bookstore.common.utils.R;
import com.star.bookstore.member.entity.AdminEntity;
import com.star.bookstore.member.entity.UserEntity;
import com.star.bookstore.member.service.AdminService;
import com.star.bookstore.member.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.Map;


/**
 * @author wuLiFei
 * @email wuLiFei@gmail.com
 * @date 2021-03-07 19:18:32
 */
@RestController
@Slf4j
@RequestMapping("/member/user")
public class UserController {
    @Autowired
    private UserService userService;

    @Autowired
    private AdminService adminService;

    //检查登录状态,决定是否拦截，被远程调用
    @RequestMapping("/login/checkByLoginNumber")
    public R getLoginStatus(@RequestParam("userId") String userId,
                            @RequestParam("loginNumber") String loginNumber) {

        log.info("远程调用成功：用户Id" + userId + "登录码=" + loginNumber);
        Long id = Long.valueOf(userId);
        if (id == 0) {
            AdminEntity admin = adminService.getById(1);
            System.out.println("登录码：" + admin.getLoginNumber());
            if (admin.getLoginNumber() != null && admin.getLoginNumber().equals(loginNumber)) {
                return R.ok().put("loginStatus", admin.getLoginStatus());
            } else {
                return R.error().put("loginStatus", false);
            }
        } else {
            UserEntity user = userService.getById(Long.valueOf(userId));
            if (user.getLoginNumber() != null && user.getLoginNumber().equals(loginNumber)) {
                return R.ok().put("loginStatus", user.getLoginStatus());
            } else {
                return R.error().put("loginStatus", false);
            }
        }
    }

    //检查是否拦截
    @GetMapping("/login/check")
    public R loginCheck(HttpServletRequest request, HttpServletResponse response) {

        String loginNumber = request.getParameter("loginNumber");
        String userId = request.getParameter("userId");

        if ("null".equals(userId) || !StringUtils.isNumeric(userId) || "null".equals(loginNumber)) {
            return R.error().put("msg", "未登录");
        }
        //检查用户登录状态
        R r = this.getLoginStatus(userId, loginNumber);
        Boolean loginStatus = (Boolean) r.get("loginStatus");
        System.out.println("状态：" + loginNumber);
        if (!loginStatus) {
            return R.error().put("msg", "未登录");
        } else {
            request.getSession().setAttribute("token1", userId);
            return R.ok().put("msg", "已登录");
        }
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = userService.queryPage(params);

        return R.ok().put("page", page);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{userId}")
    public R info(@PathVariable("userId") Long userId) {

        UserEntity user = userService.getById(userId);

        return R.ok().put("user", user);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody UserEntity user) {

        System.out.println(user.toString());
        boolean flag = userService.checkRepeat(user);

        if (flag) {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            user.setCreateTime(simpleDateFormat.format(new Date()));
            userService.save(user);
            return R.ok();
        } else {
            return R.error().put("msg", "用户名或手机号已存在");
        }

    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody UserEntity user) {

        userService.updateById(user);

        return R.ok();

    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] userIds) {

        userService.removeByIds(Arrays.asList(userIds));

        return R.ok();
    }

}
